package com.phantom.netty.server;

import com.phantom.netty.protocol.MessageCodecSharable;
import com.phantom.netty.protocol.ProtocolFrameDecoder;
import com.phantom.netty.server.handler.*;
import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.*;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.DuplexChannel;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.handler.logging.LogLevel;
import io.netty.handler.logging.LoggingHandler;
import io.netty.handler.timeout.IdleState;
import io.netty.handler.timeout.IdleStateEvent;
import io.netty.handler.timeout.IdleStateHandler;
import lombok.extern.slf4j.Slf4j;

@Slf4j
public class ChatServer {
    public static void main(String[] args) {
        NioEventLoopGroup boss = new NioEventLoopGroup();
        NioEventLoopGroup worker = new NioEventLoopGroup();
        LoggingHandler LOGGING_HANDLER = new LoggingHandler(LogLevel.DEBUG);
        MessageCodecSharable MESSAGE_CODEC = new MessageCodecSharable();
        LoginRequestMessageHandler LOGIN_HANDLER = new LoginRequestMessageHandler();
        ChatRequestMessageHandler CHAT_HANDLER = new ChatRequestMessageHandler();
        GroupCreateRequestMessageHandler GROUP_CREATE_HANDLER = new GroupCreateRequestMessageHandler();
        GroupJoinRequestMessageHandler GROUP_JOIN_HANDLER = new GroupJoinRequestMessageHandler();
        GroupMembersRequestMessageHandler GROUP_MEMBERS_HANDLER = new GroupMembersRequestMessageHandler();
        GroupQuitRequestMessageHandler GROUP_QUIT_HANDLER = new GroupQuitRequestMessageHandler();
        GroupChatRequestMessageHandler GROUP_CHAT_HANDLER = new GroupChatRequestMessageHandler();
        QuitHandler QUIT_HANDLER = new QuitHandler();

        try {
            ServerBootstrap serverBootstrap = new ServerBootstrap();
            serverBootstrap.channel(NioServerSocketChannel.class)
                        .group(boss, worker)
                        .childHandler(new ChannelInitializer<SocketChannel>() {
                            @Override
                            protected void initChannel(SocketChannel ch) throws Exception {

                                ch.pipeline().addLast(new ProtocolFrameDecoder());
                                ch.pipeline().addLast(LOGGING_HANDLER);
                                ch.pipeline().addLast(MESSAGE_CODEC);
                                // 5秒内如果没有收到channel的数据，会触发一个IdleState#READER_IDLE 事件
                                ch.pipeline().addLast(new IdleStateHandler(5,  0, 0)); // 空闲状态检测器：判断是不是读空闲时间过长，或写空闲时间过长
                                ch.pipeline().addLast(new ChannelDuplexHandler() { // 可以同时作为入栈和出栈处理器
                                    // 用来触发特殊事件
                                    @Override
                                    public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception {
                                        IdleStateEvent event = (IdleStateEvent) evt;
                                        // 触发了读空闲事件
                                        if (event.state() == IdleState.READER_IDLE) {
                                            log.debug("已经 5s 没有读到数据了！");
                                        }

                                    }
                                });
                                ch.pipeline().addLast(LOGIN_HANDLER);
                                ch.pipeline().addLast(CHAT_HANDLER);
                                ch.pipeline().addLast(GROUP_CREATE_HANDLER);
                                ch.pipeline().addLast(GROUP_JOIN_HANDLER);
                                ch.pipeline().addLast(GROUP_MEMBERS_HANDLER);
                                ch.pipeline().addLast(GROUP_QUIT_HANDLER);
                                ch.pipeline().addLast(GROUP_CHAT_HANDLER);
                                ch.pipeline().addLast(QUIT_HANDLER);

                            }
                        });

            Channel channel = serverBootstrap.bind(8080).sync().channel();
            channel.closeFuture().sync();
        } catch (InterruptedException e) {
            log.error("sever error", e);
        } finally {
            boss.shutdownGracefully();
            worker.shutdownGracefully();
        }

    }

}
